package com.dabaozhan.modules.wx_app.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.ValidationBeanUtils;
import com.dabaozhan.modules.wx_app.base.PageResult;
import com.dabaozhan.modules.wx_app.base.ResponseDto;
import com.dabaozhan.modules.wx_app.entity.FormQueryEntity;
import com.dabaozhan.modules.wx_app.entity.HistogramRequestEntity;
import com.dabaozhan.modules.wx_app.entity.HistogramResponseEntity;
import com.dabaozhan.modules.wx_app.entity.TodayDataEntity;
import com.dabaozhan.modules.wx_app.entity.FormPersonResponseEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.facade.PurchaseFormFacade;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "采购报表")
@RestController
@RequestMapping("/wx_app")
public class PurchaseFormController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(PurchaseFormController.class);
	
	@Autowired
	private PurchaseFormFacade purchaseFormFacade;

	@ApiOperation(value = "采购报表")
    @RequestMapping(value = "/getPurchase")
	public String getPurchase(FormQueryEntity entity){
		ResponseDto<List<FormPersonResponseEntity>> responseDto = new ResponseDto<List<FormPersonResponseEntity>>();
		try{
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			PageResult<List<FormPersonResponseEntity>> pageResult = purchaseFormFacade.getPurchaseList(entity);
			responseDto.setResult(pageResult.getListData());
			responseDto.setTotal(pageResult.getTotal());
			responseDto.setPage(entity.getPage());
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("查询采购报表发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("查询采购报表请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "采购报表矩形图")
    @RequestMapping(value = "/getPurchaseHistogram")
	public String getPurchaseHistogram(HistogramRequestEntity entity){
		ResponseDto<List<HistogramResponseEntity>> responseDto = new ResponseDto<List<HistogramResponseEntity>>();
		try{
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			List<HistogramResponseEntity> list = purchaseFormFacade.getPurchaseHistogram(entity);
			responseDto.setResult(list);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("查询采购报表矩形图发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("查询采购报表矩形图请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "今日数据")
    @RequestMapping(value = "/getTodayData")
	public String getTodayData(String userId){
		ResponseDto<TodayDataEntity> responseDto = new ResponseDto<TodayDataEntity>();
		try{
			TodayDataEntity result = purchaseFormFacade.getTodayData(userId);
			responseDto.setResult(result);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("查询今日数据发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("查询今日数据请求参数:{}-返回结果:{}",userId,JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}

}
